package com.xy.system.code.service.impl;

import com.xy.core.constant.JsonUtil;
import com.xy.core.constant.PathUtil;
import com.xy.core.page.PageDataResult;
import com.xy.system.source.vo.DataSourceVO;
import com.xy.system.code.service.IDataSourceService;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @Description:
 * @version: 1.0
 * @date: 2018/11/21 11:04
 */
@Service
@Primary
public class DataSourceServiceImpl implements IDataSourceService {


    public List<DataSourceVO> findDataList() {
        List<DataSourceVO> resList = new ArrayList<>();
        DataSourceVO vo;
        File dir = new File(PathUtil.dataDir());
        for (File file : dir.listFiles()) {
            try {
                if(!file.exists()){
                    continue;
                }
                String txt = new String(Files.readAllBytes(Paths.get(file.getAbsolutePath())));
                if(txt == null || txt.length() <=0 ){
                    continue;
                }
                vo = (DataSourceVO) JsonUtil.json2Java(txt, DataSourceVO.class);
                if (vo != null) {
                    resList.add(vo);
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        Collections.sort(resList, new Comparator<DataSourceVO>() {
            @Override
            public int compare(DataSourceVO o1, DataSourceVO o2) {
                return o1.getTxt().compareTo(o2.getTxt());
            }
        });
        return resList;
    }


    @Override
    public PageDataResult findPage(DataSourceVO cond, Integer pageNum, Integer pageSize) {
        PageDataResult pageDataResult = new PageDataResult();
        List<DataSourceVO> dataSourceVOList = findDataList();
        if (dataSourceVOList.size() != 0) {
            pageDataResult.setList(dataSourceVOList);
            pageDataResult.setPageSize(1000);
            pageDataResult.setPageNum(0);
            pageDataResult.setTotalPages(1);
            pageDataResult.setTotalSize(dataSourceVOList.size());
        }

        return pageDataResult;
    }

    @Override
    public List<DataSourceVO> findList(DataSourceVO cond) {
        return findDataList();
    }

    //每一条文件
    private File getFileById(Long id) {
        File file = new File(PathUtil.dataDir() + "/" + id + ".txt");
        return file;
    }


    @Override
    public DataSourceVO findById(Long id) {
        File file = getFileById(id);
        if (!file.exists()) {
            return null;
        }
        try {
            String txt = new String(Files.readAllBytes(Paths.get(file.getAbsolutePath())));
            DataSourceVO vo = (DataSourceVO) JsonUtil.json2Java(txt, DataSourceVO.class);
            return vo;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    @Override
    public int updateData(DataSourceVO cond) {
        File file = getFileById(cond.getId());
        if (!file.exists()) {
            file.getParentFile().mkdirs();
        }
        String txt = JsonUtil.java2Json(cond);
        try {
            Files.write(Paths.get(file.getAbsolutePath()), txt.getBytes());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return 1;
    }

    @Override
    public int deleteData(Long id) {
        File file = getFileById(id);
        if (file.exists()) {
            file.delete();
        }
        return 1;
    }

    @Override
    public int addData(DataSourceVO cond) {
        cond.setId(System.currentTimeMillis());
        File file = getFileById(cond.getId());
        if (!file.exists()) {
            file.getParentFile().mkdirs();
        }
        String txt = JsonUtil.java2Json(cond);
        try {
            Files.write(Paths.get(file.getAbsolutePath()), txt.getBytes());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return 1;
    }
}
